//
//  YWNavigationController.m
//  通信录
//
//  Created by yellow on 16/6/28.
//  Copyright © 2016年 yellow. All rights reserved.
//

#import "YWNavigationController.h"


@interface YWNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation YWNavigationController

////系统在第一次使用这个类的时候调用
//+(void)initialize{
//
//    //1.设置导航栏主题
//    [self setupNavBarTheme];
//
//    //2.设置导航栏按钮主题
//    [self setupBarButtonItemTheme];
//
//
//}



-(void)viewDidLoad{
    [super viewDidLoad];

    self.modalPresentationStyle = 0;
    
    [YWNavigationController setupNavBarTheme];
    [YWNavigationController setupBarButtonItemTheme];

    
    //全局手势优化返回上一级控制器
    id target = self.interactivePopGestureRecognizer.delegate;

    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")];
    self.pan = pan;
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
   
//因为有了FDFullscreenPopGesture、所以这个新添加的手势基本没用了
    // 给导航控制器的view添加全屏滑动手势
    [self.interactivePopGestureRecognizer.view addGestureRecognizer:pan];

    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]){
        
        return NO;
    }
    else{
        return YES;
        
    }
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    //#warning - 测试（做了YWLiveController的判断好像可以）
    //因为YWLiveController与pageView demo的手势会冲突
    //但是假如你YWLiveController不用pageView的demo，用普通的，就不需要这个判断，因为YWLiveController好像做了固定播放器Y值，都不需要pageView的demo的功能了
    //不过好像它既然固定了Y值，就不会上下滚动，应该这里不做判断也不会引起上下滚动和左右手势的冲突
    
//        id topVC = [self.viewControllers lastObject];
//
//        YWLog(@"%@",NSStringFromClass([topVC class]));
        
//        if ([topVC isKindOfClass:[YWLiveController class]]) {
//            return NO;
//        }
    
    

    if (self.viewControllers.count > 1) {
        return YES;
    }

    return NO;
}

//重写pushViewController(用storyboard的话这个方法不适用)
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    //push的时候隐藏tabbar
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
    
}

//1.设置导航栏主题
+(void)setupNavBarTheme{
    UINavigationBar *navBar = [UINavigationBar appearance];
    [navBar setBarTintColor:[UIColor whiteColor]];
    [navBar setTintColor:[UIColor blackColor]];

    //设置标题属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:17];
    [navBar setTitleTextAttributes:textAttrs];

    //取消模糊效果防止色差
    navBar.translucent = NO;
    
    //bar背景颜色
     UIImage *img = [YWNavigationController createImageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)];
    [navBar setBackgroundImage:img  forBarMetrics:UIBarMetricsDefault];

}

//2.设置导航栏按钮主题
+(void)setupBarButtonItemTheme{
    UIBarButtonItem *item = [UIBarButtonItem appearance];

    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];

}
+(UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return theImage;
}
//
////重写pushViewController(用storyboard的话这个方法不适用)
//-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//
//    //左边返回按钮不显示文字
//    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] init];
//    barButtonItem.title = @"";
//    viewController.navigationItem.backBarButtonItem = barButtonItem;
//
//    //push的时候隐藏tabbar
//    if (self.viewControllers.count > 0) {
//        viewController.hidesBottomBarWhenPushed = YES;
//    }
//    [super pushViewController:viewController animated:animated];
//}



@end
